package net.sos.playground;

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexTest {

   public static void main(final String[] args) {
      new RegexTest().run();
   }
   
   public void run() {      
      Scanner in = new Scanner(System.in);
      String input = null, pattern = null;
      while(true) {
         System.out.print("Enter the pattern: ");
         if(in.hasNext()) {
            pattern = in.next();
         }
         System.out.print("Enter the input: ");
         if(in.hasNext()) {
            input = in.next();
         }
         if(testExistence(input, pattern)) {
            System.out.println("Match found\n");
         } else {
            System.out.println("No Match found\n");
         }
      }
   }
   
   /**
    * Test the existence of the <code>pattern</code> in the given
    * <code>input</code>.
    */
   private boolean testExistence(String input, String pattern) {
      input = "sanjay sharma is da besto dude";
      pattern = "best";
      pattern = Pattern.quote(pattern);
      Pattern p = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE);
      Matcher matcher = p.matcher(input);
      return matcher.find();
   }

}
